package self;

public class XianCheng {
    public static void main(String[] args) {
        TestThread testThread = new TestThread();
        testThread.threadMethod();
//        RunnableImpl runnable = new RunnableImpl();
//        runnable.runnableMethod();
    }
}

// Thread 创建线程
class ShareDate{
    public static String szData = "";
}
class TestThread extends Thread{
    private ShareDate oShare;
    TestThread(){}
    TestThread(String szName, ShareDate oShare){
        super(szName);
        this.oShare = oShare;
    }
    @Override
    public void run() {
        synchronized (oShare){
            for (int i=0;i<5;i++){
                if (this.getName().equals("Thread1")){
                    oShare.szData = "这是第 1 个线程";
                    try {
                        Thread.sleep((int)Math.random()*50);
                    }catch (InterruptedException e){
                    }
                    System.out.println(this.getName()+" : "+oShare.szData);
                }else if (this.getName().equals("Thread2")){
                    oShare.szData = "这是第 2 个线程";
                    try {
                        Thread.sleep((int)Math.random()*50);
                    }catch (InterruptedException e){
                    }
                    System.out.println(this.getName()+" : "+oShare.szData);
                }
            }
//            for (int row=1;row<=9;row++){
//                for (int i=1;i<=row;i++){
//                    System.out.print(i+"*"+row+"="+i*row+"\t");
//                }
//                System.out.println();
//            }
        }
    }
    public void threadMethod(){
        ShareDate oShare = new ShareDate();
        TestThread testThread = new TestThread("Thread1", oShare);
        TestThread testThread1 = new TestThread("Thread2", oShare);
        testThread.start();
        testThread1.start();
//        TestThread testThread = new TestThread();
//        TestThread testThread1 = new TestThread();
//        TestThread testThread2 = new TestThread();
//        testThread.start();
//        testThread1.start();
//        testThread2.start();
    }
}

// Runnable 创建线程
class RunnableImpl implements Runnable{
    @Override
    public void run() {
        for(int row=1;row<=9;row++){
            for (int col=1;col<=row;col++){
                System.out.print(col+"*"+row+"="+col*row+"\t");
            }
            System.out.println();
        }
    }
    public void runnableMethod(){
        Runnable runnable = new RunnableImpl();
        Runnable runnable1 = new RunnableImpl();
        Runnable runnable2 = new RunnableImpl();
        Thread thread = new Thread(runnable);
        Thread thread1 = new Thread(runnable1);
        Thread thread2 = new Thread(runnable2);
        thread.start();
        thread1.start();
        thread2.start();
    }
}